//
//  LZManageReviewsViewController.m
//  laziz_Merchant
//
//  Created by xbn_iMac on 2016/12/18.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZManageReviewsViewController.h"
#import "UIViewController+NavigationItemSetting.h"
#import "UIViewController+InitWithdDefaultNib.h"
#import "HTHorizontalSelectionList.h"
#import "LZManageReviewsBGScrollView.h"
#import "LZManageReviewsVideosTableView.h"
#import "LZManageReviewsRestaurantReviewsTableView.h"
#import "LZManageReviewsVideoReviewsViewController.h"
#import "LZManageReviewsPresenter.h"
#import "LZCommonWebViewController.h"

#import "Masonry.h"

@interface LZManageReviewsViewController ()<HTHorizontalSelectionListDelegate, HTHorizontalSelectionListDataSource, LZHorizontalBaseSubTableViewDelegate,LZManageReviewsRestaurantReviewsTableViewDelegate>

@property (nonatomic, strong) HTHorizontalSelectionList * headerListView;

@property (nonatomic, strong) LZManageReviewsBGScrollView *scroll;

@property (nonatomic, strong) NSArray *carMakes;

@property (nonatomic, strong) LZManageReviewsPresenter *manageReviewsPresenter;

@end

@implementation LZManageReviewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.umLogPageViewName = @"评价管理页面";
    
    self.title = LZLocalizedString(@"home_menu_manage_reviews", nil);
    
    [self configNavigationBarButton];
    
    [self configDataBase];
    
    [self configUI];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - request

/* 请求优惠视频列表 */
- (void)requestVideoListDateWithPrameter:(NSDictionary *)prameter andPage:(int)page andLimit:(int)limit resultSuccess:(void (^)(NSArray *, id))successBlock {
    [self.manageReviewsPresenter promotionVideoListWithPage:page andLimit:limit onSuccess:^(id data) {
        if (successBlock) {
            successBlock(data,nil);
        }
    } onFail:^(NSString *fail) {
        if (successBlock) {
            successBlock(nil,nil);
        }
    }];
}

/* 评论管理--餐厅评论信息 */
- (void)requestCommentCountSuccess:(void (^)(id))successBlock {
    [self.manageReviewsPresenter restaurantCommentInfoOnSuccess:^(id data) {
        if (successBlock) {
            successBlock(data);
        }
    } onFail:^(NSString *fail) {
        if (successBlock) {
            successBlock(nil);
        }
    }];
}

/* 评价管理--商户评价列表接口 */
- (void)requestRestaurantReviewListWithPrameter:(NSDictionary *)prameter andPage:(int)page andLimit:(int)limit resultSuccess:(void (^)(NSArray *, id))successBlock {
    
    // 评论类型（0全部，1好评，2差评；不填为全部）
    NSString *type = prameter[@"type"];
    
    [self.manageReviewsPresenter restaurantReviewListWithType:type andPage:page andLimit:limit onSuccess:^(id data) {
        if (successBlock) {
            successBlock(data,nil);
        }
    } onFail:^(NSString *fail) {
        if (successBlock) {
            successBlock(nil,nil);
        }
    }];
}

/* 评论列表--添加回复 */

- (void)requestReplyReviewsWithReviewsUserId:userId andCommentId:commentId andReplies:replies resultSuccess:(void (^)(id))successBlock {
    WS(weakSelf)
    [self startLoadingViewForView:self.view loadingViewUserInteractionEnabled:YES];
    [self.manageReviewsPresenter replyReviewsWithMessageType:@"2" andReviewsUserId:userId andCommentId:commentId andReplies:replies onSuccess:^(id data) {
        [weakSelf stopLoadingViewForView:weakSelf.view];
        if (successBlock) {
            successBlock(data);
        }
        
    } onFail:^(NSString *fail) {
        [weakSelf stopLoadingViewForView:weakSelf.view];
        if (successBlock) {
            successBlock(nil);
        }
    }];
}

/* 评论列表--删除评论 */
- (void)requestDeleteReplyWithReplyId:(NSString*)replyId resultSuccess:(void (^)(id))successBlock {
    WS(weakSelf)
    [self startLoadingViewForView:self.view loadingViewUserInteractionEnabled:YES];
    
    [self.manageReviewsPresenter deleteReplyReviewsWithReplyId:replyId onSuccess:^(id data) {
        [weakSelf stopLoadingViewForView:weakSelf.view];
        if (successBlock) {
            successBlock(data);
        }
    } onFail:^(NSString *fail) {
        [weakSelf stopLoadingViewForView:weakSelf.view];
        if (successBlock) {
            successBlock(nil);
        }
    }];
}

#pragma mark - config data base

- (void)configDataBase {
    self.carMakes = @[LZLocalizedString(@"review_videos", nil), LZLocalizedString(@"review_restaurant", nil)];
    self.manageReviewsPresenter = [[LZManageReviewsPresenter alloc] init];
}

#pragma mark - configUI method
// 初始化提交按钮
- (void)configNavigationBarButton {

    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setTitle:LZLocalizedString(@"review_rating_rules", nil) forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [rightButton setTitleColor:UIColorFromRGB(0xffffff, 1) forState:UIControlStateNormal];
    [rightButton sizeToFit];
    rightButton.frame = CGRectMake(0, 0, CGRectGetWidth(rightButton.bounds), 40);
    [rightButton addTarget:self action:@selector(navigationBarRightButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self navigationItemSetting:@[rightButton] type:NAVIGATIONITEMSETTING_RIGHT];
}

- (void)configUI {
    
    // *********************** 头部的选择按钮View ***********************
    self.headerListView = [[HTHorizontalSelectionList alloc] init];
    [self.headerListView setScrollViewColor:UIColorFromRGB(0xffffff, 1)];
    self.headerListView.delegate = self;
    self.headerListView.dataSource = self;
    [self.view addSubview:self.headerListView];
    
    [self.headerListView makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
        make.height.mas_equalTo(55);
    }];

    // *********************** 主ScrollView ***********************
    _scroll = [[LZManageReviewsBGScrollView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.scroll];
    
    [self.scroll makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headerListView.mas_bottom);
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    [self configTableViewsDelegate];
}

- (void)configTableViewsDelegate {
    self.scroll.videoTable.subTableViewDelegate = self;
    self.scroll.restaurantsReviewTable.subTableViewDelegate = self;
    self.scroll.restaurantsReviewTable.reviewTableDelegate = self;
    
    [self.scroll.videoTable checkHaveData];
}


#pragma mark - Event
// 评分规则（导航栏右边按钮）点击事件
- (void)navigationBarRightButtonClickAction:(UIButton*)sender {
    DLog(@"评分规则");
    LZCommonWebViewController *aboutUsVc = [[LZCommonWebViewController alloc]initWithDefaultNib];
    aboutUsVc.webCode = kWebState_ratingRules;
    [self.navigationController pushViewController:aboutUsVc animated:YES];
}

#pragma mark - HTHorizontalSelectionListDataSource Protocol Methods

- (NSInteger)numberOfItemsInSelectionList:(HTHorizontalSelectionList *)selectionList {
    return self.carMakes.count;
}

- (NSString *)selectionList:(HTHorizontalSelectionList *)selectionList titleForItemWithIndex:(NSInteger)index {
    return self.carMakes[index];
}

#pragma mark - HTHorizontalSelectionListDelegate Protocol Methods

- (void)selectionList:(HTHorizontalSelectionList *)selectionList didSelectButtonWithIndex:(NSInteger)index {
    // update the view for the corresponding index
    
    DLog(@"select : %ld", (long)index);
    [self.scroll clickButtonTag:index];
}

#pragma mark - LZHorizontalBaseSubTableViewDelegate

- (void)requestDateWithTag:(NSInteger)tag andPrameter:(NSDictionary *)prameter andPage:(int)page andLimit:(int)limit resultSuccess:(void (^)(NSArray *, id))successBlock {
    if (tag == 1) {
        [self requestVideoListDateWithPrameter:prameter andPage:page andLimit:limit resultSuccess:successBlock];
    }
    else if (tag == 2) {
        [self requestRestaurantReviewListWithPrameter:prameter andPage:page andLimit:limit resultSuccess:successBlock];
    }
}

- (void)tableViewTag:(NSInteger)tag didSelectRowAtIndexPath:(NSIndexPath *)indexPath object:(id)object {
    if (tag == 1) {
        LZManageReviewsVideoReviewsViewController *vc = [[LZManageReviewsVideoReviewsViewController alloc] initWithDefaultNib];
        vc.restaurantModel = object;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - LZManageReviewsRestaurantReviewsTableViewDelegate
- (void)requestCommentCountResultSuccess:(void (^)(id))successBlock {
    [self requestCommentCountSuccess:successBlock];
}

- (void)requestAddReplyWithReviewsUserId:(NSString *)userId andCommentId:(NSString *)commentId andReplies:(NSString *)replies ResultSuccess:(void (^)(id))successBlock {
    [self requestReplyReviewsWithReviewsUserId:userId andCommentId:commentId andReplies:replies resultSuccess:successBlock];
}

- (void)requestDeleteWithReplyReplyId:(NSString *)replyId ResultSuccess:(void (^)(id))successBlock {
    [self requestDeleteReplyWithReplyId:replyId resultSuccess:successBlock];
}

@end
